function toast(content, icon = "none", duration = 1500) {
  return new Promise((resolve, reject) => {
    uni.showToast({
      icon: icon,
      title: content,
      duration: duration,
      success: (res) => {
        uni.$u.sleep(duration).then(() => {
          resolve(res);
        });
      },
      fail: (err) => {
        reject(err);
      },
    });
  });
}
/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
function showConfirm(content) {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: "提示",
      content: content,
      cancelText: "取消",
      confirmText: "确定",
      success: function (res) {
        if(res.confirm){
          resolve(res);
        }
      },
    });
  });
}
/**
 * 参数处理
 * @param params 参数
 */
function tansParams(params) {
  let result = "";
  for (const propName of Object.keys(params)) {
    const value = params[propName];
    var part = encodeURIComponent(propName) + "=";
    if (value !== null && value !== "" && typeof value !== "undefined") {
      if (typeof value === "object") {
        for (const key of Object.keys(value)) {
          if (
            value[key] !== null &&
            value[key] !== "" &&
            typeof value[key] !== "undefined"
          ) {
            let params = propName + "[" + key + "]";
            var subPart = encodeURIComponent(params) + "=";
            result += subPart + encodeURIComponent(value[key]) + "&";
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&";
      }
    }
  }
  return result;
}
// token 和 存储
function getToken() {
  return uni.getStorageSync("token");
}
function setStorage(name, key) {
  return uni.setStorageSync(name, key);
}
function getStorage(key) {
  return uni.getStorageSync(key);
}
function removeStorage(key) {
  return uni.removeStorageSync(key);
}
// 根据参数名称获取值
function GetQueryValue(queryName, url) {
  var reg = new RegExp("(^|&)" + queryName + "=([^&]*)(&|$)", "i");
  let searchArr = url.split("?");
  var r = searchArr.length > 1 ? searchArr[1].match(reg) : null;
  if (r != null) {
    return decodeURI(r[2]);
  } else {
    return null;
  }
}
// 解决精度不准 加减乘除
class Cal {
  jia(num1, num2) {
    return new Decimal(num1).add(new Decimal(num2)).toNumber();
  }
  jian(num1, num2) {
    return new Decimal(num1).sub(new Decimal(num2)).toNumber();
  }
  cheng(num1, num2) {
    return new Decimal(num1).mul(new Decimal(num2)).toNumber();
  }
  chu(num1, num2) {
    return new Decimal(num1).div(new Decimal(num2)).toNumber();
  }
}
const cal = new Cal();
// 获取上一页的实例
function getBackPage(methodName, ...args) {
  // 获取当前页面栈
  const pages = getCurrentPages();
  console.log("pages: ", pages);
  // 如果页面栈长度小于2，说明没有上一页
  if (pages.length < 2) {
    console.error("No previous page found.");
    return;
  }
  // 获取上一个页面的实例
  const prevPage = pages[pages.length - 2];
  console.log(prevPage.$vm?.$.exposed);
  // 检查上一个页面是否暴露了我们想要调用的方法
  if (!prevPage.$vm?.$?.exposed?.[methodName]) {
    console.error(`Method ${methodName} not exposed by the previous page.`);
    return;
  }
  // 调用上一个页面的暴露方法，并传递任何参数
  prevPage.$vm?.$?.exposed[methodName](...args);
}
// 全局图片地址拼接
function loadImg(url) {
  return config.imgUrl + url;
}

export default {
  toast,
  showConfirm,
  tansParams,
  getToken,
  setStorage,
  getStorage,
  removeStorage,
  GetQueryValue,
  cal,
  getBackPage,
  loadImg,
};
